﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Super.Common.Cache;

namespace Super.Common
{
    public class DataCache
    {
        #region 公共缓存数据

        private static readonly object threadLock = new object();

        /// <summary> 公共缓存数据
        /// </summary>
        private static readonly SmartCache<object> data = new SmartCache<object>(int.MaxValue);

        private static SmartCache<object> Data
        {
            get { return data; }
        }

        /// <summary> 添加或设置缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void Set(string key, object value, TimeSpan? timeOutSpan = null)
        {
            if (timeOutSpan == null)
            {
                data.Add(key, value);
            }
            else
            {
                data.Add(key, value, timeOutSpan.Value);
            }
        }

        /// <summary> 获取缓存,如果不存在,则返回null
        /// 
        /// 调用方法
        /// DataCache.Get("startDate", () => DateTime.Now,TimeSpan.FromDay(3));
        /// 
        /// 将来扩展Key由object构成.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        public static object Get(string key, Func<object> func = null, TimeSpan? timeOutSpan = null)
        {
            //暂时没加并发锁. 方法还需扩展修改.
            object o;

            if (!data.TryGetValue(key, out o))
            {
                if (timeOutSpan == null)
                {
                    timeOutSpan = TimeSpan.FromDays(1024);
                }


                if (func != null) //添加委托判断
                {
                    o = func();
                    data.Add(key, o, timeOutSpan.Value);
                }
            }
            return o;
        }

        public static void Remove(string key)
        {
            data.Remove(key);
        }

        /// <summary> 移除满足情况的参数.
        /// 
        /// //移除所有Key和Value 的length 大于 3 的所有缓存.
        /// Remove((key, value) => key.Length > 3 && value.ToString().Length > 3)
        /// 
        /// </summary>
        /// <param name="func">第一个参数,key 第二个参数 value 返回是否删除</param>
        public static void Remove(Func<string, object, bool> func)
        {
            lock (threadLock)
            {
                List<string> ls = data.Items.Where(j => func(j.Key, j.Value.Value.Value)).Select(j => j.Key).ToList();

                foreach (string item in ls)
                {
                    data.Remove(item);
                }
            }
        }

        #endregion

        /// <summary> 供非Web程序使用
        /// </summary>
        public static object __dBContext;

        /// <summary> 数据上下文对象
        /// </summary>
        public static object DbContext
        {
            get { return HttpContext.Current != null ? HttpContext.Current.Items["__dbContext"] : __dBContext; }
            set { if (HttpContext.Current != null) HttpContext.Current.Items["__dbContext"] = value; else  __dBContext = value; }
        }

        /// <summary> 校验类别是否为静态缓存类别. 包括全表以及片段.
        /// </summary>
        public static Func<Type, bool> CheckTypeIsStaticType { get; set; }

        /// <summary> 基与请求的缓存
        /// </summary>
        public static IDictionary HttpContextCache { get { return HttpContext.Current.Items; } }

        /// <summary> 当前Controller
        /// </summary>
        public static Controller NowController
        {
            get { return HttpContextCache["__NowController"] as Controller; }
            set { HttpContextCache["__NowController"] = value; }
        }
    }
}